package com.cy.controller;


import com.cy.common.Constant;
import com.cy.entity.Comment;
import com.cy.entity.DiscussPost;
import com.cy.entity.Event;
import com.cy.event.EventProducer;
import com.cy.service.CommentService;
import com.cy.service.DiscussPostService;
import com.cy.util.HostHolder;
import com.cy.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.Configuration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  评论
 * </p>
 *
 * @author cy
 * @since 2021-11-08
 */
@RequestMapping("/comment")
@Controller
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private HostHolder hostHolder;

    @Resource
    private EventProducer eventProducer;

    @Resource
    private DiscussPostService discussPostService;

    @Autowired
    private EventProducer producer;

    @Autowired
    private RedisTemplate redisTemplate;


    // 添加评论
    @PostMapping(path = "/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        //触发评论事件  发送评论通知
        Event event = new Event();
        event.setTopic(Constant.TOPIC_COMMENT);
        event.setUserId(hostHolder.getUser().getId());
        event.setEntityType(comment.getEntityType());
        event.setEntityId(comment.getEntityId());
        event.setData("postId", discussPostId);

        if(comment.getEntityType() == Constant.ENTITY_TYPE_POST){
            DiscussPost target = discussPostService.getById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }else if(comment.getEntityType() == Constant.ENTITY_TYPE_COMMENT){
            Comment target = commentService.getById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        producer.fireMessage(event);


        //评论给帖子的评论才发送EL  评论给评论的评论不发送
        if(comment.getEntityType() == Constant.ENTITY_TYPE_POST){
            event = new Event();
            event.setTopic(Constant.TOPIC_PUBLIC);
            event.setUserId(comment.getUserId());
            event.setEntityType(Constant.ENTITY_TYPE_POST);
            event.setEntityId(Integer.valueOf(discussPostId));
            producer.fireMessage(event);


            // 计算帖子分数
            redisTemplate.opsForSet().add(RedisKeyUtil.getPostScoreKey(), discussPostId);
        }

        return "redirect:/discuss-post/detail/" + discussPostId;
    }

}
